{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# list的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Alex', 'Marry', 'Jan', 'Jack', 'danmin', 'mingjin']\n"
     ]
    }
   ],
   "source": [
    "# 1.取值：切片\n",
    "# 2.增加/新增数据\n",
    "names = [\"Alex\",\"Marry\",\"Jan\",\"Jack\"]\n",
    "# append() 圆括号的内容是对象（比较大的一个范围，python中一切皆是对象）\n",
    "# append()新增在末尾\n",
    "names.append(\"danmin\")\n",
    "names.append(\"mingjin\")\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Alex', 'Marry', 'pengmin', 'Jan', 'Jack', 'danmin', 'mingjin']\n"
     ]
    }
   ],
   "source": [
    "# 2.2 insert()指定位置插入新增数据\n",
    "names.insert(2,\"pengmin\")\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Alex', 'Marry', 'dan', 'Jan', 'danmin', 'mingjin']\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "list.remove(x): x not in list",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-8-56e8421fe65a>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0mnames\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Jack\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnames\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0mnames\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"11\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      6\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnames\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: list.remove(x): x not in list"
     ]
    }
   ],
   "source": [
    "# 3.删除 \n",
    "# 3.1 remove()的操作应该先做查询，判断元素value是否存在？\n",
    "names.remove(\"Jack\")\n",
    "print(names)\n",
    "names.remove(\"11\")\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "# * 先做查询：seq的一般方法（通用方法）\n",
    "names = [\"Alex\",\"Marry\",\"Jan\",\"Jack\"]\n",
    "print(\"lihua\" in names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "username = input (\"names:\")\n",
    "if username  in names:\n",
    "    names.remove(username)\n",
    "else:\n",
    "    names.append(username)\n",
    "print(names)\n",
    "names.remove(\"lihua\")\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. 正向取值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Silver', 'Yibo']\n",
      "['Baek', 'Silver']\n",
      "['Baek', 'Silver']\n"
     ]
    }
   ],
   "source": [
    "names = [\"Baek\",\"Silver\",\"Yibo\",\"pcy\"]\n",
    "# list 取值 ”：“ 右边的值取不到 例如[1:3] 3这个位置取不到\n",
    "print(names[1:3]) #切片 取出中间一些连续的值（还是列表）\n",
    "print(names[0:2])\n",
    "# 如果 左侧的值是 即从0开始取值，0可以省略不写\n",
    "print(names[:2]) #取出某一个值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.反向取值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pcy\n",
      "['Silver', 'Yibo']\n",
      "['Yibo', 'pcy']\n",
      "['Baek', 'Silver']\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "# list 取值 ”：“ 右边的值取不到 例如-3:-1] -1这个位置取不到\n",
    "#注意: 取到最好一个值，不能写 0 省略不写\n",
    "print(names[-1]) #切片 取出最后一个值\n",
    "print(names[-3:-1]) #切片 从后往前取出一些值（还是列表），思考为什么不是[-1:-3]?\n",
    "print(names[-2:]) #切片 取到最后一个值\n",
    "print(names[:2]) #切片 同上，即“0”可以省略\n",
    "\n",
    "print(names[-3:1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
      "[1, 3, 5]\n"
     ]
    }
   ],
   "source": [
    "num = [1,2,3,4,5,6,7,8,9]\n",
    "# 对比 range\n",
    "print(list(range(10)))\n",
    "print(num[:6:2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
